//
//  CSBaseTableViewController.m
//  classroom
//
//  Created by Macx on 16/7/30.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "CSBaseTableViewController.h"

@interface CSBaseTableViewController ()

@end

@implementation CSBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ((self.dataArray!= nil) && (self.dataArray.count>0)) {
        LZSection * sec = self.dataArray[indexPath.section];
        LZItem * it = sec.items[indexPath.row];
        if (it.block!=nil) {
            it.block();
        }else if ([it isKindOfClass:[LZAccess class]]){
            
            LZAccess * item = (LZAccess *) it;
            if (item.nibName) {
                UIStoryboard * sb = [UIStoryboard storyboardWithName:item.nibName bundle:nil];
                UIViewController * vc = [sb instantiateInitialViewController];
                vc.title = item.title;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            
        }
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ((self.dataArray!= nil) && (self.dataArray.count>0)) {
        LZSection * group = self.dataArray[section];
        return  group.items.count;
    }else{
        return 1;
    }
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ((self.dataArray!= nil) && (self.dataArray.count>0)) {
        return self.dataArray.count;
    }
    else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LZSetTableCell * cell = [LZSetTableCell cellWithTableView:tableView];
    if ((self.dataArray!= nil) && (self.dataArray.count>0)) {
        LZSection * group = self.dataArray[indexPath.section];
        LZItem * item = group.items[indexPath.row];
        cell.item=item;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if ((self.dataArray!= nil) && (self.dataArray.count>0)) {
        LZSection * group = self.dataArray[section];
        if (group.footerLabel) {
            return group.footerLabel;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if ((self.dataArray!= nil) && (self.dataArray.count>0)) {
        LZSection * group = self.dataArray[section];
        if (group.headerLabel) {
            return group.headerLabel;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}
-(NSArray *)dataArray{
    if (_dataArray==nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
